package com.yunxi.service.dubboService.impl.account;

import com.github.pagehelper.PageInfo;
import com.yunxi.account.pojo.AccountTypeVO;
import com.yunxi.account.service.AccountTypeDubboService;
import com.yunxi.core.util.BeanTools;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.account.AccountType;
import com.yunxi.service.localService.account.AccountTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 帐户类型 服务实现类
 * </p>
 *
 * @author
 * @since 2019-05-18
 */
//@com.alibaba.dubbo.config.annotation.Service(version = "${demo.service.version}",
//        application = "${dubbo.application.id}",
//        protocol = "${dubbo.protocol.id}",
//        registry = "${dubbo.registry.id}"
//)
public class AccountTypeDubboServiceImpl implements AccountTypeDubboService {

   @Autowired
   @Qualifier(value = "accountTypeServiceImpl")
   AccountTypeService accountTypeService;


   @Override
   public AccountTypeVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      AccountType accountType = accountTypeService.getById(id);
      AccountTypeVO accountTypeVO = new AccountTypeVO();
      BeanUtils.copyProperties(accountType, accountTypeVO);
      return accountTypeVO;
   }

   @Override
   public void saveOrUpdate(List<AccountTypeVO> accountTypeVOList, LoginInfo loginInfo) throws InvocationTargetException, IllegalAccessException {
      if (accountTypeVOList!=null && accountTypeVOList.size()>0){
         for (AccountTypeVO accountTypeVO : accountTypeVOList) {
            if (BeanTools.isAllFieldNull(accountTypeVO)) continue;
            if (StringUtils.isEmpty(accountTypeVO.getAccountTypeId())) {
               accountTypeVO.setAccountTypeId(UUID.randomUUID().toString().replaceAll("-", ""));
               accountTypeVO.setCreateBy(loginInfo.getRealName());
            }
            accountTypeVO.setUpdateBy(loginInfo.getRealName());
            AccountType accountType = new AccountType();
            BeanUtils.copyProperties(accountTypeVO, accountType);
            accountTypeService.saveOrUpdate(accountType);
         }
      }

   }

   @Override
   public void removeById(String id, LoginInfo loginInfo) {
      AccountType accountType = accountTypeService.getById(id);
      accountType.setUpdateBy(loginInfo.getRealName());
      accountTypeService.removeById(id);
   }

   @Override
   public PageInfo<AccountTypeVO> findPageByVO(AccountTypeVO accountTypeVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {

      PageInfo<AccountTypeVO> pageInfo = accountTypeService.pageByVo(accountTypeVO, pageNum, pageSize);

      return pageInfo;

   }

   @Override
   public AccountTypeVO load(String accountTypeCode) {

      return accountTypeService.load(accountTypeCode);
   }

   @Override
   public List listByVO(AccountTypeVO accountTypeVO) {
      return accountTypeService.listByVo(accountTypeVO);
   }

   @Override
   public ApiResult listAccountType(AccountTypeVO accountTypeVO, LoginInfo loginInfo) {
      List<AccountTypeVO> list = null;
      if (org.apache.commons.lang3.StringUtils.isNotEmpty(accountTypeVO.getPartnerCode())){
         list = accountTypeService.listAccountType(accountTypeVO);
      }
      return ResponseUtil.ok(list);
   }

}
